﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations
{
    class ConfigurationSettingCollection : IConfigurationCollection
    {
        int index = -1;

        List<IConfiguration> configs = new List<IConfiguration>();

        public IConfiguration GetConfiguration(string id)
        {
            if (string.IsNullOrEmpty(id))
                throw new Exception("missing input id.");

            id = id.Trim().ToUpper();

            foreach (IConfiguration config in configs)
            {
                if (string.Equals(config.Id, id, StringComparison.OrdinalIgnoreCase))
                    return config;
            }

            return null;
        }

        public IConfiguration GetConfiguration(string id, string name)
        {
            if (string.IsNullOrEmpty(id))
                throw new Exception("missing input id.");

            if (string.IsNullOrEmpty(name))
                throw new Exception("missing input name.");

            id = id.Trim().ToUpper();

            name = name.Trim().ToUpper();

            foreach (IConfiguration config in configs)
            {
                if (!string.Equals(config.Id, id, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!string.Equals(config.Name, name, StringComparison.OrdinalIgnoreCase))
                    continue;

                return config;
            }

            return null;
        }

        public void Add(IConfiguration setting)
        {
            if (setting == null)
                throw new Exception("missing config.");

            string id = setting.Id;

            string name = setting.Name;

            if (string.IsNullOrEmpty(id))
                throw new Exception("missing config id.");

            if (Contains(id, name))
                throw new Exception("config id already existed. id = " + id);

            this.configs.Add(setting);
        }

        public int Count
        {
            get
            {
                return configs.Count;
            }
        }

        public IConfiguration GetConfiguration(int index)
        {
            if (index < 0 || index > configs.Count - 1)
                throw new Exception("index exceed config.count = " + configs.Count);

            return configs[index];
        }

        public bool Contains(string id)
        {
            if (string.IsNullOrEmpty(id))
                return false;

            foreach (IConfiguration config in configs)
            {
                if (string.Equals(id, config.Id, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public bool Contains(string id, string name)
        {
            if (string.IsNullOrEmpty(id))
                return false;

            if (string.IsNullOrEmpty(name))
                return Contains(id);

            foreach (IConfiguration config in configs)
            {
                if (!string.Equals(id, config.Id, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!string.Equals(name, config.Name, StringComparison.OrdinalIgnoreCase))
                    continue;

                return true;
            }

            return false;
        }

        public override string ToString()
        {
            return "configuration.count = " + configs.Count;
        }

        #region IEnumerable<IConfiguration> 成员

        public IEnumerator<IConfiguration> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IConfiguration> 成员

        public IConfiguration Current
        {
            get
            {
                return GetConfiguration(index);
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return GetConfiguration(index);
            }
        }

        public bool MoveNext()
        {
            if (index >= configs.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion
    }
}
